'''
寻找全排列的下一个数
字典序算法
'''


def find_nearest_number(number=[]):
    index = find_transfer_point(number)
    if index == 0:
        return None
    number_copy = number.copy()
    exchange_head(index, number_copy)

    reverse(index, number_copy)
    return number_copy


def find_transfer_point(number=[]):
    for i in range(len(number) - 1, 0, -1):
        if number[i] > number[i - 1]:
            return i
    return 0


def exchange_head(index, number=[]):
    head = number[index - 1]
    for i in range(len(number) - 1, 0, -1):
        if head < number[i]:
            number[index - 1] = number[i]
            number[i] = head
            break
    return number


def reverse(index, number=[]):
    i = index
    j = len(number) - 1
    while i < j:
        temp = number[i]
        number[i] = number[j]
        number[j] = temp
        i += 1
        j -= 1
    return number


def output_number(number=[]):
    for i in number:
        print(i, end='')
    print()


my_number = list([1, 2, 4, 5, 3])
my_number1 = find_nearest_number(my_number)
output_number(my_number1)
